export const thumbnail = (src: string, callback: (img: string) => void, w: number, h: number) => {
  const canvas = document.createElement('canvas');
  const ctx = canvas.getContext('2d');
  const img = new Image();
  img.crossOrigin = 'Anonymous';
  (w = w || 0), (h = h || 0);
  img.src = src;
  img.onload = function () {
    //为传入缩放尺寸用原尺寸
    !w && (w = img.width);
    !h && (h = img.height);
    // 以长宽最大值作为最终生成图片的依据
    if (w !== img.width || h !== img.height) {
      var ratio;
      if (w > h) {
        ratio = img.width / w;
        h = img.height / ratio;
      } else if (w === h) {
        if (img.width > img.height) {
          ratio = img.width / w;
          h = img.height / ratio;
        } else {
          ratio = img.height / h;
          w = img.width / ratio;
        }
      } else {
        ratio = img.height / h;
        w = img.width / ratio;
      }
    }
    // 以传入的长宽作为最终生成图片的尺寸
    if (w > h) {
      var offset = (w - h) / 2;
      canvas.width = canvas.height = w;
      ctx?.drawImage(img, 0, offset, w, h);
    } else if (w < h) {
      var offset = (h - w) / 2;
      canvas.width = canvas.height = h;
      ctx?.drawImage(img, offset, 0, w, h);
    } else {
      canvas.width = canvas.height = h;
      ctx?.drawImage(img, 0, 0, w, h);
    }
    callback(canvas.toDataURL('image/png'));
  };
};
